home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: new-news.sprintlink.net!eskimo!scs
- From: scs@eskimo.com (Steve Summit)
- Subject: Re: How to detect EOF?/filepointer problem.
- X-Nntp-Posting-Host: eskimo.com
- Message-ID: <Dpv413.LtC@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: schmorganization
- References: <316F8932.2BAF@www.partio.fi>
- Date: Sun, 14 Apr 1996 17:17:26 GMT
-
- In article <316F8932.2BAF@www.partio.fi>, "R.K.Brand"
- <ralph@www.partio.fi> writes:
- > ...I have a problem while scanning through a file too see
- > whether or not a certain word occurs. When I run out of file,
- > fscanf() keeps returning the last line, instead of some NULL value...
- > How can I test for the end of the file?
-
- fscanf() never returns pointers, so it can't return NULL.
- It will return EOF at end-of-file, as long as nothing weird is
- going on. But weird things do go on with scanf. If you're
- reading lines, a line-based input function such as fscanf is
- usually a much better choice.
-
- (If you're reading words using fscanf and %s, and it has reached
- end-of-file, it's likely that your word buffer will still contain
- a copy of the last word read, regardless of whether fscanf tried
- to tell you it reached EOF and regardless of whether you noticed.)
-
- You didn't mention feof(), but if you happen to be using it, make
- sure you call it *after* calling an input function that might
- have detected EOF, not before. (C's I/O is not like Pascal's.
- feof() does not detect EOF; it merely reports whether some input
- function has detected it.)
-
- > The second problem is how to manipulate a pointer of type
- > "FILE"..so, a filepointer. At a certain point I want to skip
- > 11 chars back in the file and start writing. string-=11; gives
- > me an error and after string--; fprintf(string,"foo"); does not
- > do anything anymore.
-
- FILE pointers are not strings, they're, well, file pointers.
- In fact, you can't manipulate them at all (they're supposed to be
- an "opaque" type); everything you do with a FILE pointer, you do
- by passing that FILE pointer to a library function (or macro).
- To move around in the stream, use fseek(). Beware, though, that
- it's not portable to use fseek() to "skip 11 characters back" in
- a text file; strictly speaking, fseek() on text files may only be
- used to seek to the beginning or end of the file or to a position
- returned by a prior call to ftell().
-
- > I you respond to this one, please do also mail me a copy.
-
- Tell you what: how about I send you mail telling you you can find
- a copy here? (Or is your news feed extremely unreliable?)
-
- Steve Summit
- scs@eskimo.com
-